import axios from 'axios';
import { showToast } from 'vant';

import { showLoadingToast, closeToast } from 'vant';


const service = axios.create({
  baseURL: 'https://api.wmdsm.cn',
  timeout: 5000,
  headers: {
    'X-Wrap-Response': 'true' // 默认值
  }
});

// 请求拦截器
service.interceptors.request.use(
  (config) => {
    showLoadingToast({
      message: '加载中...',
      forbidClick: true // 禁止背景点击
    });
    // config.headers.Authorization = 'Bearer token'; // 添加token
    return config;
  },
  (error) => Promise.reject(error)
);

// 响应拦截器
service.interceptors.response.use(
  (response) => {
    closeToast()
    if(response.data?.code === 200) {
      return response.data
    }else {
      showToast(response.data?.msg || '网络开了个小差，请稍后重试~~~')
      return Promise.reject("");
    }
  }, // 直接返回数据
  (error) => {
    closeToast()
    console.error('响应错误:', error);
    return Promise.reject(error);
  }
);

export default service;
